/*=============================================================================
#    The element-comm is a tentative program used to test our ideas.           
#                       Copyright (C)  bgao                                    
#
#     This program is free software; you can redistribute it and/or            
#      modify it under the terms of the GNU General Public License             
#     as published by the Free Software Foundation; either version 2           
#         of the License, or (at your option) any later version.               
#
#     This program is distributed in the hope that it will be useful,          
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            
#             GNU General Public License for more details.                    
#
#    You should have received a copy of the GNU General Public License        
#     along with this program; if not, write to the Free Software             
#      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,             
#                       MA  02110-1301, USA.                                  
#==============================================================================
# $Id$ 
#
# $Author$ 
#
# $Revision$ 
#
# $Date$ 
#
# Description: 
#
=============================================================================*/
 #include "rtos-wsn.h"
 #include "test-thread-event.h"
#include    "wsn-taskid.h"
#include "led.h"

static struct wsn_event test_thread_event;
struct wsn_event edfi_test_event;
static struct wsn_event wsn_thread12_event;
static struct wsn_event wsn_thread34_event;
static struct wsn_semaphore wsn_thread34_sem;


CONTROL_OUT_TEOUT(void, set)(uint16);

void backtask1_entry(void);


void delay_ticks (int ticks)
{
    uint32 time;
    int32 delta;
    
    time = WSN_GetSystemTime();
    do {
		delta = WSN_GetSystemTime() - time;
    } while (delta < ticks);
}


void thread1_entry(void)
{
    while ( 1 ) {
		WSN_WaitEvent(&test_thread_event, -1);
		/** Thread is running ...1 **/
        WSN_debug(0x02e4, 0);
		//CONTROL_TEOUT(set)(1);
		led_toggle(LED_RED);
		delay_ticks(400);
#if 1		
		/* EDFI test */
		WSN_NewTask(TEST_TASK3ID, 0, 0);
		/* EDFI test */

		/* thread test */
		/** Thread 1 set wsn_thread12_event **/
		WSN_debug(0x02db, 0);
		WSN_SetEvent(&wsn_thread12_event);
		/* thread test */

#endif		
		/** Thread Goto sleep ...1 **/
		WSN_debug(0x02d1, 0);
		WSN_Sleep(CONSTAINT_MINI_TO_TICKS(4000));
    }
}

void thread2_entry(void)
{
    while ( 1 ) {
		WSN_WaitEvent(&test_thread_event, -1);
		/** Thread is running ...2 **/
        WSN_debug(0x02e5, 0);
		//CONTROL_TEOUT(set)(2);
		led_toggle(LED_RED);
		delay_ticks(500);
#if 1		
		/* thread test */
		/** Thread 2 wait wsn_thread12_event **/
		WSN_debug(0x02dc, 0);
		WSN_WaitEvent(&wsn_thread12_event, -1);
		/** Thread 2 got wsn_thread12_event **/
		WSN_debug(0x02dd, 0);
		/* thread test */
#endif		
		/** Thread Goto sleep ...2 **/
		WSN_debug(0x02d2, 0);
		WSN_Sleep(CONSTAINT_MINI_TO_TICKS(3000));
    }
}

void thread3_entry(void)
{
    while ( 1 ) {
		WSN_WaitEvent(&test_thread_event, -1);
		/** Thread is running ...3 **/
        WSN_debug(0x02e6, 0);
		//CONTROL_TEOUT(set)(4);
		led_toggle(LED_RED);
		delay_ticks(600);
#if 1		
		/* thread test */
		/** Thread 3 wait Sem wsn_thread34_sem **/
		WSN_debug(0x02de, 0);
		WSN_WaitSem(&wsn_thread34_sem, -1);
		/** Thread 3 got Sem wsn_thread34_sem **/
		WSN_debug(0x02df, 0);
		WSN_Sleep(CONSTAINT_MINI_TO_TICKS(4000));
		/** Thread 3 post Sem wsn_thread34_sem **/
		WSN_debug(0x02e0, 0);
		WSN_SignalSem(&wsn_thread34_sem);
		/* thread test */
#endif
		/** Thread Goto sleep ...3 **/
		WSN_debug(0x02d3, 0);
		WSN_Sleep(CONSTAINT_MINI_TO_TICKS(4000));
    }
}

void thread4_entry(void)
{
    while ( 1 ) {
		WSN_WaitEvent(&test_thread_event, -1);		
		//CONTROL_TEOUT(set)(7);
		led_toggle(LED_RED);
		/** Thread is running ...4 **/
        WSN_debug(0x02e7, 0);
		wsn_backtask_post(backtask1_entry);
		delay_ticks(400);
#if 1
		/* thread test */
		/** Thread 4 wait Sem wsn_thread34_sem **/
		WSN_debug(0x02e1, 0);
		WSN_WaitSem(&wsn_thread34_sem, -1);
		/** Thread 4 got Sem wsn_thread34_sem **/
		WSN_debug(0x02e2, 0);
		WSN_Sleep(CONSTAINT_MINI_TO_TICKS(5000));
		/** Thread 4 post Sem wsn_thread34_sem **/
		WSN_debug(0x02e3, 0);
		WSN_SignalSem(&wsn_thread34_sem);
		/* thread test */
#endif		
		/** Thread Goto sleep ...4 **/
		WSN_debug(0x02d4, 0);		
		WSN_Sleep(CONSTAINT_MINI_TO_TICKS(5000));
    }
}


void backtask1_entry(void)
{
	/**backtask1 post by thread **/
	WSN_debug(0x02ce, 0);
}

void CONTROL_IN_TEIN(init)(uint16 mode, uint16 flags)//CONTROL_THREADEVENT_THINint
{
	//WSN_InitEvent(&test_thread_event, 0, 0);
	WSN_InitEvent(&edfi_test_event, 1, 0);
	WSN_InitEvent(&wsn_thread12_event, 1, 0);
	WSN_InitEvent(&wsn_thread34_event, 1, 0);
	WSN_InitSem(&wsn_thread34_sem, 1);
}


void CONTROL_IN_TEIN(start)(uint16 flags)//CONTROL_THREADEVENT_THINstart
{
	WSN_SetEvent(&test_thread_event);
}


void CONTROL_IN_TEIN(stop)(uint16 flags)//CONTROL_THREADEVENT_THINstop
{
	WSN_ClearEvent(&test_thread_event);
}
